<template>
	<view class="content">
		<uni-card class="card">
			<textarea class="text" v-model="pass.oldpass" placeholder="请输入原始密码">
			</textarea>
		</uni-card>
		<uni-card class="card">
			<textarea class="text" v-model="pass.newpass" placeholder="请输入新密码">
			</textarea>
		</uni-card>
		<view class="btn">
			<button class="button" @click="submit()">提交
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pass:{
					username: '',
					oldpass: '',
					newpass:'',
					}
			}
		},
		methods: {
			onPullDownRefresh() {
				console.log('刷新页面');
				setTimeout(() => {
					uni.stopPullDownRefresh();
				}, 1000)
			},
			getJwtToken() {
				try {
					return wx.getStorageSync('jwt_token');
				} catch (e) {
					return null;
				}
			},
			sendRequestWithToken(url, method, data) {
				const token = this.getJwtToken();
				if (token) {
					uni.request({ // 使用 uni.request 而不是 wx.request  
						url: url,
						method: method,
						data: data,
						header: {
							'token': token // 确保服务器期望的 header 键是 'token'  
						},
						success: (res) => {
							if (res.data && res.data.code === 1 && res.data.msg === 'success') {
								uni.showToast({
									title: '密码修改成功成功',
									icon: 'success'   
								});
							} else if (res.data && res.data.code === 0) {
								uni.showToast({
									title: '登录信息有误',
									icon: 'none' 
								});
								this.goPage(0); // 确保 goPage 方法存在且可以正确处理页面跳转  
								console.error('未登录', res.data.data);
							} else {
								// 处理其他返回码或异常情况  
								console.error('请求成功但返回了未知的结果', res.data);
							}
						},
						fail: (error) => {
							// 处理请求失败的情况  
							console.error('请求失败', error);
						}
					});
				} else {
					// 没有有效的令牌时的处理逻辑  
					console.error('没有有效的令牌');
				}
			},
			getuserName() {
				try {
					return wx.getStorageSync('user_name');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			submit() {
				this.pass.username = this.getuserName();

				const url = 'http://localhost:8080/patra/setpass';
				const method = 'POST';
				const data = this.pass
				this.sendRequestWithToken(url, method, data);
				this.pass.oldpass = null;
				this.pass.newpass = null;


			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.card {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 200rpx;
		height: 200rpx;
		width: 90%;
		background-color: #F3F3F3;
	}
	.text{
		height: 150rpx;
		width: 90%;
	}

	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 300rpx;
		height: 70rpx;
		border-radius: 30rpx;
		margin-top: 70rpx;
		margin-bottom: 100rpx;
	}

	.button {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 300rpx;
		height: 70rpx;
		border-radius: 50rpx;
		background-color: #585CE5;
		font-family: AliMamaShuHei;
		color: #F3F3F3;
		font-size: 28rpx;

	}
</style>